/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import HibernateUtil.HibernateUtil;
import Pojo.Addrobj;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author knyazev_o
 */
public class ServiceFIAS {
    private Session session;
    private Transaction transaction;
    
    private final String defaultRegion = "23";
    
    private Map<String, Addrobj> regions;
    private Map<String, Addrobj> countys;
    private Map<String, Addrobj> districts;
    private Map<String, Addrobj> citys;
    private Map<String, Addrobj> innerCitys;
    private Map<String, Addrobj> localitys;
    private Map<String, Addrobj> streets;
    private Map<String, Addrobj> addGrounds;
    private Map<String, Addrobj> streetAddGrounds;
    
    public ServiceFIAS(){
        this.regions  = AOLevel("select * from CONTRACT.ADDROBJ a where a.aolevel=1");
    }

    public void RegionId(String GUId){
        countys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=2");
        districts = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=3");
        citys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=4");
    }
    
    public void DistrictId(String GUId) {            
        citys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=4");
        innerCitys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=5");
        localitys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=6");
    }
    
    public void CityId(String GUId) {          
        innerCitys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=5");
        localitys = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=6");
        streets = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=7");
    }
   
    public void InnerCityId(String GUId) { 
        streets = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=7");
    }
    
    public void LocalityId(String GUId) {        
        streets = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=7");
        addGrounds = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=9");
    }
    
    public void AddGroundId(String GUId) {
        streetAddGrounds = AOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+GUId+"' and a.aolevel=10");
    }
        
    private Map<String, Addrobj> AOLevel(String dbQuery) {
        
        this.session=null;
        this.transaction=null;        
        try
        {
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            Query query = session.createSQLQuery(dbQuery).addEntity(Addrobj.class); 
            //return query.list();
            Map<String, Addrobj> resultsMap = new HashMap<String, Addrobj>();
            for (Object o : query.list()) {
                Addrobj addr = (Addrobj) o;
                resultsMap.put(addr.getAoguid(), addr);
            }
            return resultsMap;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            System.out.println("Фатальная ошибка: " + ex.getMessage());
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
        return null;
    }

    public String getDefaultRegion() {
        return defaultRegion;
    }

    public Map<String, Addrobj> getRegions() {
        return regions;
    }

    public Map<String, Addrobj> getCountys() {
        return countys;
    }

    public Map<String, Addrobj> getDistricts() {
        return districts;
    }

    public Map<String, Addrobj> getCitys() {
        return citys;
    }

    public Map<String, Addrobj> getInnerCitys() {
        return innerCitys;
    }

    public Map<String, Addrobj> getLocalitys() {
        return localitys;
    }

    public Map<String, Addrobj> getStreets() {
        return streets;
    }

    public Map<String, Addrobj> getAddGrounds() {
        return addGrounds;
    }

    public Map<String, Addrobj> getStreetAddGrounds() {
        return streetAddGrounds;
    }
   
    
}
